<?php

class Note
{
	var $content;
	var $username;
	var $book;
	var $vote;
	var $devote;
	var $ctime;
	var $id;
	
	public function Note($book_,$username_,$content_,$vote_,$devote_,$time_,$id_)
	{
		$this->content = $content_;
		$this->username = $username_;
		$this->book = $book_;
		$this->vote = $vote_;
		$this->devote = $devote_;
		$this->ctime = $time_;
		$this->id = $id_;
	}
	
	public static function getNotesByUser($username)
	{
		$DB = mysql_connect("localhost","root","12345");
		mysql_query("set names utf8");
		mysql_select_db("mynotes",$DB);
		$DBT = mysql_query("SELECT * FROM notes WHERE notesUser = '$username'");
		$results = array();
		for ($i=0; $i< mysql_num_rows($DBT); $i++)
		{			
		  $row = mysql_fetch_array($DBT);
		  $comment = new Note($row['bookname'],$row['notesUser'],$row['content'],$row['tops'],$row['downs'],$row["time"],$row["id"]);
		  	
		  $results[] = $comment;
		}  	
		return $results;
  }
  
  public static function getNotesByBook($bookname)
  {
  	$DB = mysql_connect("localhost","root","12345");
	mysql_query("set names utf8");
  	mysql_select_db("mynotes",$DB);
  	$DBT = mysql_query("SELECT * FROM notes WHERE bookname = '$bookname'");
  	
  	$results = array();
  	
  	for ($i=0; $i < mysql_num_rows($DBT); $i++)
  	{
  		$row = mysql_fetch_array($DBT);
  		$comment = new Note($row['bookname'],$row['notesUser'],$row['content'],$row['tops'],$row['downs'],$row["time"],$row["id"]);
  		$results[] = $comment;
  	}
  	return $results;
  }
  
  public static function getnotesById($flag)
  {
  	$DB = mysql_connect("localhost","root","12345");
	mysql_query("set names utf8");
  	mysql_select_db("mynotes",$DB);
  	$DBT = mysql_query("SELECT * FROM notes WHERE id= '$flag'");
	if(mysql_num_rows($DBT) == 0)
	{
		die("no notes.");
	}
	else 
	{
		$DBTrow = mysql_fetch_array($DBT);
		$comment = new Note($DBTrow['bookname'],$DBTrow['notesUser'],$DBTrow['content'],$DBTrow['tops'],$DBTrow['downs'],$DBTrow["time"],$DBTrow["id"]);
	}
	return $comment;
  }
  
  public static function newContent($bkname,$usname,$c)
  {
  	$currentTime = date("Y-m-d H:i:s");
	// echo $currentTime;
  	$newC = new Note($bkname,$usname,$c,0,0,$currentTime,0);
	$newC->writeToDB();
  }
  
  public function writeToDB()
  {
  	// echo "connect";
  	$DB = mysql_connect("localhost","root","12345");
	mysql_query("set names utf8");
  	mysql_select_db("mynotes",$DB);
  	$flag = mysql_query("INSERT INTO notes (bookname,notesUser,content,tops,downs,time) VALUES ('$this->book','$this->username','$this->content','$this->vote','$this->devote','$this->ctime')"); 
 	// echo $flag;
  }
  
  public function getContent()
  {
  	return $this->content;
  }
  public function getBookName()
  {
  	return $this->book;
  }
  public function getUserName()
  {
  	return $this->username;
  }
  public function getVote()
  {
  	return $this->vote;
  }
  public function getDevote()
  {
  	return $this->devote;
  }
  public function getTime()
  {
  	return $this->ctime;
  }
  public function getId()
  {
  	return $this->id;
  }
  public function Vote()
  {
  	mysql_query("update notes set tops = tops + 1 where id = '$this->id'");
  	$this->vote++;
  }
  public function Devote()
  {
  	mysql_query("update notes set downs = downs + 1 where id = '$this->id'");
  	$this->devote++;
  }
  
};
?>